package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.Impl.UserServiceImpl;
import com.atguigu.service.UserService;
import com.atguigu.utils.WebUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @ClassName UserServlet
 * @Description 继承BaseServlet，执行父类的doPost方法
 * @Author 周轩霆
 * @Date 2022/4/15 13:56
 **/

public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * @return void
     * @Author 周轩霆
     * @Description ；利用反射调用相关方法
     * @Date 15:03 2022/4/15
     * @Param [req, resp]
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    /**
     * @return void
     * @Author 周轩霆
     * @Description 登录方法
     * @Date 14:18 2022/4/15
     * @Param [req, resp]
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User loginUser = userService.login(new User(null, username, password, null));
        if (loginUser != null) {
            //把错误信息，和回显的表单项信息，保存到Request域中
            //保存用户登录之后的信息
            req.getSession().setAttribute("user", loginUser);
            //跳到登录成功页面
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);

        } else {

            //登录失败，到登录页面
            req.setAttribute("msg", "用户名或者密码错误!");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        }


    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        req.getSession().invalidate();
        resp.sendRedirect("/book/index.jsp");//注意地址的特殊情况

    }


    /**
     * @return void
     * @Author 周轩霆
     * @Description 注册方法
     * @Date 14:19 2022/4/15
     * @Param [req, resp]
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        //获取Session域中生成的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除Session域中生成的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        //用于注入参数
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());

        //检查验证码是否正确-----先写死
        if (token!=null&&token.equalsIgnoreCase(code)) {//忽略大小写
            //检查用户名
            if (userService.existsUsername(username)) {
                System.out.println("用户名[" + username + "]已存在！");
                req.setAttribute("msg", "用户名已存在！");
                req.setAttribute("username", username);
                req.setAttribute("email", email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
                //保存到数据库user
                userService.registerUser(user);
                //将用户信息保存到Session域中
                req.getSession().setAttribute("user", user);
                //用于在注册成功界面显示已经注册的用户名，设置2秒是为了让用户到首页去登录
                req.getSession().setMaxInactiveInterval(2);
                //到成功页面
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
            }

        } else {
            System.out.println("验证码[" + code + "]错误");
            //跳转页面
            //把回显信息保存到request域中
            req.setAttribute("msg", "验证码错误");
            req.setAttribute("username", username);
            req.setAttribute("email", email);
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }
    }
}

